package com.forum.business;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.forum.model.pojo.ForumUser;
import com.forum.model.pojo.Post;
import com.forum.model.pojo.Topic;

public class ForumManager {

	public static ForumUser fillUserData(HttpServletRequest request) {
		ForumUser user = new ForumUser();
		user.setFirstName(request.getParameter("first_name"));
		user.setLastName(request.getParameter("last_name"));
		user.setLoginName(request.getParameter("user_name"));
		user.setPassHash(request.getParameter("user_password"));
		user.setBirthDate(0, 0, 0);
		user.setGender(request.getParameter("gender").charAt(0));
		user.setCountry(request.getParameter("no country"));
		user.setCity(request.getParameter("no city"));
		user.setEmail(request.getParameter("user_email"));
		user.setAvatarImage("no image");
		user.setPostCount(0);
		user.setRank('n');
		user.setShortBio("no infromation");
		user.setSignature("no signature");

		return user;
	}

	public static ForumUser fillEditedUserData(HttpServletRequest request) {
		ForumUser user = (ForumUser) request.getSession().getAttribute("user");
		user.setFirstName(request.getParameter("first_name"));
		user.setLastName(request.getParameter("last_name"));
		String pass = request.getParameter("user_password");
		if (pass != null && !pass.isEmpty()) {
			user.setPassHash(pass);
		}
		user.setGender(request.getParameter("gender").charAt(0));
		user.setCountry(request.getParameter("country"));
		user.setCity(request.getParameter("city"));
		user.setEmail(request.getParameter("user_email"));
		user.setShortBio(request.getParameter("shortBio"));
		user.setSignature(request.getParameter("signature"));

		return user;
	}

	public static Post fillPostData(HttpServletRequest request) {
		ForumUser user = (ForumUser) request.getSession().getAttribute("user");
		Post post = new Post();
		post.setTitle(request.getParameter("post_title"));
		post.setDescription(request.getParameter("post_description"));
		post.setContent(request.getParameter("content"));
		post.setPostOwner(user.getId());
		Date date = new Date();
		post.setStartDate(date);
		post.setModifyDate(date);
		return post;
	}

	public static Topic fillTopicData(HttpServletRequest request) {
		ForumUser user = (ForumUser) request.getSession().getAttribute("user");
		Topic topic = new Topic();
		topic.setSectionName((String) request.getParameter("sectionName"));
		topic.setTopicOwner(user.getId());
		topic.setTitle(request.getParameter("post_title"));
		topic.setDescription(request.getParameter("post_description"));
		topic.setStatusFlag('o');
		Date date = new Date();
		topic.setStartDate(date);
		topic.setModifyDate(date);
		topic.setPostCount(1);
		return topic;
	}
}
